package controller;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.AddcontactView;
import view.ListView;
import model.Adressbook;
import model.Contact;

/**
 * 
 * @author Nelly-Kim TRAN
 * 
 * @param pastfirstname
 * 					to keep trace of the previous firstname registered (in case it is modified here)
 * @param pastlastname
 * 					to keep trace of the previous lastname registered (in case it is modified here)
 * @param view
 * 					to get all the new data of the contacts
 * @param theview
 * 					to allow some actions not permitted by the class ModifyListener (dispose)
 * 
 *
 */

public class RegisterListener implements ActionListener{
	
	String pastfirstname;
	String pastlastname;
	ModifyListener view;
	JFrame theview;
	


	public RegisterListener(String pastfirstname, String pastlastname,ModifyListener view, JFrame theview) {
		super();
		this.pastfirstname = pastfirstname;
		this.pastlastname = pastlastname;
		this.view = view;
		this.theview=theview;
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		
		//remove the contact from the set
		Iterator i=Adressbook.contacts.iterator();
		while(i.hasNext()) {
			Contact c=(Contact)i.next();
			if( c.getFirstname().equals(pastfirstname) && c.getLastname().equals(pastlastname) ) {
				System.out.println("1st name match" + c.getFirstname() );
				System.out.println("last name match" + c.getLastname() );
				i.remove();
			}
		}
		
		//add the contact to the set with the modifications		
		Adressbook.contacts.add(new Contact(view.getLabel1().getText(),view.getLabel2().getText(),view.getLabel3().getText(),view.getLabel4().getText(),view.getLabel5().getText(),view.getLabel6().getText(),view.getLabel7().getText(),view.getLabel8().getText(),view.getLabel9().getText(),view.getLabel10().getText(),
				view.getLabel11().getText(),view.getLabel12().getText(),view.getLabel13().getText()));
		
		//refresh the list in the file
		try{
			File yourFile = new File("monfichier2.txt");
			boolean bool=yourFile.delete();
			File yourNewFile = new File("monfichier2.txt");
			yourNewFile.createNewFile(); 
			
			PrintStream l_out = new PrintStream(new FileOutputStream("monfichier2.txt",true));	
			Iterator j=Adressbook.contacts.iterator();
			while(j.hasNext()) {
				Contact c=(Contact)j.next();
				l_out.print(c.getFirstname()+ ","); 
				l_out.print(c.getLastname()+","); 
				l_out.print(c.getHomeaddress()+","); 
				l_out.print(c.getCompanyaddress()+","); 
				l_out.print(c.getPersonalmobile()+","); 
				l_out.print(c.getPersonalhome()+","); 
				l_out.print(c.getProfessionnalmobile()+","); 
				l_out.print(c.getProfessionnaloffice()+","); 
				l_out.print(c.getHomepage()+","); 
				l_out.print(c.getPersonalemail()+","); 
				l_out.print(c.getProfessionalemail()+","); 
				l_out.print(c.getPhoto()+","); 
				l_out.print(c.getGroup()+"\r\n"); 	
				System.out.println("Ok");
			}
			
			l_out.flush(); 
			l_out.close(); 
			l_out=null; 
	
		}catch (Exception exc) {System.out.println("problem with txt file");}
		
		theview.setVisible(false);
		theview.dispose();
		ListView f= new ListView();
		f.setVisible(true);
	
		
		
		
	}

}
